/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UsernameAndPasswordPanel.java
 *
 * Created on 26 juin 2010, 13:31:06
 */

package darknet.gui.wizard.firstrun;

import java.awt.event.KeyEvent;

/**
 *
 * @author Yoyo
 */
public class UsernameAndPasswordPanel extends javax.swing.JPanel {

     /** Reference to the wizard Dialog. */
    private WizardFirstRunDialog wizard;

    /** Creates new form UsernameAndPasswordPanel */
    public UsernameAndPasswordPanel(WizardFirstRunDialog wizard) {
        this.wizard = wizard;
        initComponents();
    }

    /**
     * Check username validity and enable/disable components.
     */
    private boolean checkUsername() {

        boolean isValid = false;

        if (textFieldUsername.getText().length() != 0) {

            String username = textFieldUsername.getText();
            if (!username.matches("[a-z0-9]*")) {
                btnNext.setEnabled(false);
                labelFeedback.setText(
                        "Only alphanumeric lowercase characters for username.");
            } else {
                labelFeedback.setText("");
                btnNext.setEnabled(true);
                isValid = true;
            }
        } else {
            labelFeedback.setText("");
            btnNext.setEnabled(true);
            isValid = false;
        }

        return isValid;
    }

    /**
     * Check password validity and enable/disable components.
     */
    private boolean checkPassword() {

        boolean isValid = false;

        if (passwordField.getPassword().length != 0) {
            String password = new String(passwordField.getPassword());
            if (!password.matches("[A-Za-z0-9]{6,}")) {
                btnNext.setEnabled(false);
                labelFeedback.setText("Only alphanumeric characters for password and 6 minimum characters.");
            } else {
                labelFeedback.setText("");
                btnNext.setEnabled(true);
                isValid = true;
            }
        } else {
             labelFeedback.setText("");
             btnNext.setEnabled(true);
             isValid = false;
        }
        return isValid;
    }

    private void next() {

        if (checkUsername() && checkPassword()) {
            wizard.setUsername(textFieldUsername.getText());
            wizard.setPassword(new String(passwordField.getPassword()));
            wizard.showNetwork();
        } else {
            btnNext.setEnabled(false);
            labelFeedback.setText("Please check your username and password");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        wizardIcon = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        btnNext = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        textFieldUsername = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        btnPrevious = new javax.swing.JButton();
        labelFeedback = new javax.swing.JLabel();

        wizardIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/darknet/gui/wizard/firstrun/wizard.png"))); // NOI18N

        jEditorPane1.setContentType("text/html");
        jEditorPane1.setEditable(false);
        jEditorPane1.setText("<html>\r\n  <head>\r\n  </head>\r\n  <body>\r\n  <h3>Username and Password (1/3)</h3>\n  <p>\n\tYou have to choose a username and a password.<br />\n\t<br />\n\t\n  </p>\n  </body>\r\n</html>\r\n");
        jScrollPane2.setViewportView(jEditorPane1);

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        jLabel1.setText("Username :");

        jLabel2.setText("Password :");

        textFieldUsername.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textFieldUsernameKeyReleased(evt);
            }
        });

        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                passwordFieldKeyReleased(evt);
            }
        });

        btnPrevious.setText("Previous");
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        labelFeedback.setForeground(new java.awt.Color(255, 51, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(wizardIcon)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textFieldUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                            .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelFeedback, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(wizardIcon)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(labelFeedback, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(textFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        next();
}//GEN-LAST:event_btnNextActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
       wizard.showWelcome();
    }//GEN-LAST:event_btnPreviousActionPerformed

    private void textFieldUsernameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldUsernameKeyReleased
        checkUsername();
    }//GEN-LAST:event_textFieldUsernameKeyReleased

    private void passwordFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordFieldKeyReleased

        checkPassword();

       if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
           next();
       }
    }//GEN-LAST:event_passwordFieldKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelFeedback;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JTextField textFieldUsername;
    private javax.swing.JLabel wizardIcon;
    // End of variables declaration//GEN-END:variables

}
